package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.union.api.common.model.ApiMockInstanceCase;
import com.gitee.zycra.union.api.common.vo.InstanceCaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zycra
 */
@Repository
public class ApiMockInstanceCaseDao extends BaseDao<ApiMockInstanceCase> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 插入mock实例期望
     *
     * @param apiMockInstanceCase 实例对象
     * @return 影响行数
     */
    public int insert(ApiMockInstanceCase apiMockInstanceCase) {
        return insertIntoTable(apiMockInstanceCase);
    }

    /**
     * 批量插入mock实例期望
     *
     * @param apiMockInstanceCaseList 实例对象
     * @return 影响行数
     */
    public int batchInsert(List<ApiMockInstanceCase> apiMockInstanceCaseList) {
        return batchInsertIntoTable(apiMockInstanceCaseList);
    }

    /**
     * 根据ID更新
     *
     * @param apiMockInstanceCase 实例期望对象
     * @return 影响行数
     */
    public int updateById(ApiMockInstanceCase apiMockInstanceCase) {
        return updateTableById(apiMockInstanceCase);
    }

    /**
     * 根据实例ID和接口ID查询
     *
     * @param instanceId  实例ID
     * @param interfaceId 接口ID
     * @return 查询结果
     */
    public ApiMockInstanceCase selectByInstanceAndInterfaceId(Long instanceId, Long interfaceId) {
        return selectOneTableByNotNullColumnEquals(new ApiMockInstanceCase().setInstanceId(instanceId).setInterfaceId(interfaceId));
    }

    /**
     * 根据实例ID查询实例期望
     *
     * @param instanceId 模板ID
     * @return 查询结果
     */
    public List<InstanceCaseVO> selectMockInstanceCase(Long instanceId) {
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT interface_id, case_id FROM api_mock_instance_case WHERE instance_id = ?",
                InstanceCaseVO.class, List.of(instanceId));
    }
}
